home *** CD-ROM | disk | FTP | other *** search
/ CD BIT 75 / CD BIT 75.iso / Software / SinfoSeguros / MSDE / MSM / REPL.MSM / MergeModule.CABinet / sp3ServUni.sql.185C1D8F_1545_4277_BB64_857D2622DB57 < prev    next >
Encoding:
Text (UTF-16)  |  2002-12-11  |  102.5 KB  |  1,796 lines

  1. /*------------------------------------------------------------------------------
  2.  
  3. SP3_SERV.SQL
  4.  
  5. THIS SCRIPT TAKES THE SERVER-SIDE SYSTEM-PROCS FROM 8.0SP2 to SP3.
  6.  
  7. Changes in this file are organized as follows (please maintain):
  8.     System Tables (UPGRADE.SQL)
  9.     System Messages (MESSAGES.SQL / MSGWORK.SQL / SERVMSGS.SQL)
  10.     Engine System Procs (U_TABLES.SQL / PROCSYST.SQL / ODSOLE.SQL)
  11.     Schema Procs (OLEDBSCH.SQL / ANSIVIEW.SQL)
  12.     ODBC/OLEDB Catalog Procs (INSTCAT.SQL)
  13.     SEM SQLDMO System Procs (SQLDMO.SQL)
  14.  
  15. Changes to these scripts should NOT be placed in this file:
  16.     Starfighter Procs (XPSTAR.SQL / INSTMSDB.SQL / SQLTRACE.SQL / WEB.SQL)
  17.     Doc's Samples (INSTPUBS.SQL / INSTNWND.SQL)
  18.     Replication Procs (REPLSYS.SQL / REPLCOM.SQL / REPLTRAN.SQL / REPLMERG.SQL)
  19. These components will maintain separate upgrade scripts.
  20.  
  21. Notes:
  22. + Catalog-updates and sp_MS_upd_sysobj_category are enabled for the entire
  23.     file.  Do not disable or re-enable them.  Please do not change set options.
  24.  
  25. ------------------------------------------------------------------------------*/
  26.  
  27.  
  28. --------------------------------------------------------------------------------
  29. -- VERIFY Server is started in single-user-mode (catalog-updates enables), and
  30. --    start marking of system-objects.
  31. --------------------------------------------------------------------------------
  32. execute sp_configure 'allow updates',1
  33. go
  34.  
  35. reconfigure with override
  36. go
  37.  
  38. exec sp_MS_upd_sysobj_category 1
  39. go
  40.  
  41. --------------------------------------------------------------------------------
  42. --    System Tables (UPGRADE.SQL)
  43. --------------------------------------------------------------------------------
  44.  
  45. IF OBJECT_ID('sysprocesses') IS NOT NULL
  46.     DROP TABLE sysprocesses
  47. go
  48. CREATE TABLE sysprocesses (     -- see ntdbms\storeng\dfs\startup\dataserv.c
  49.     spid            smallint NOT NULL,
  50.     kpid            smallint NOT NULL,
  51.     blocked         smallint NOT NULL,
  52.     waittype        binary(2) NOT NULL,
  53.     waittime        int NOT NULL,
  54.     lastwaittype    nchar(32) NOT NULL,
  55.     waitresource    nchar(256) NOT NULL,
  56.     dbid            smallint NOT NULL,
  57.     uid             smallint NOT NULL,
  58.     cpu             int NOT NULL,
  59.     physical_io     bigint NOT NULL,
  60.     memusage        int NOT NULL,
  61.     login_time      datetime NOT NULL,
  62.     last_batch      datetime NOT NULL,
  63.     ecid            smallint NOT NULL,
  64.     open_tran        smallint NOT NULL,
  65.     status          nchar(30) NOT NULL,
  66.     sid             binary(86) NOT NULL,
  67.     hostname        nchar(128) NOT NULL,
  68.     program_name    nchar(128) NOT NULL,
  69.     hostprocess     nchar(8) NOT NULL,
  70.     cmd             nchar(16) NOT NULL,
  71.     nt_domain       nchar(128) NOT NULL,
  72.     nt_username     nchar(128) NOT NULL,
  73.     net_address     nchar(12) NOT NULL,
  74.     net_library     nchar(12) NOT NULL,
  75.     loginame        nchar(128) NOT NULL,
  76.     context_info    binary(128) NOT NULL,
  77.     sql_handle        binary(20) NOT NULL,
  78.     stmt_start        int NOT NULL,
  79.     stmt_end        int NOT NULL
  80.    )
  81. go
  82. GRANT SELECT ON sysprocesses TO PUBLIC
  83. go
  84. delete from sysconfigures where config = 400 and comment = N'Cross DB Ownership Chaining'
  85. go
  86. INSERT sysconfigures (config, status, value, comment) VALUES ( 400,  1 ,     0, N'Cross DB Ownership Chaining' )
  87. go
  88.  
  89. --------------------------------------------------------------------------------
  90. --    System Messages (MESSAGES.SQL / MSGWORK.SQL / SERVMSGS.SQL) English
  91. --------------------------------------------------------------------------------
  92. DELETE sysmessages
  93.  WHERE error in (
  94.      211
  95.     ,569
  96.     ,570
  97.     ,1229
  98.     ,1620
  99.     ,1621
  100.     ,1836
  101.     ,3124
  102.     ,4454
  103.     ,4455
  104.     ,5600
  105.     ,5601
  106.     ,14360
  107.     ,14361
  108.     ,14362
  109.     ,14363
  110.     ,14364
  111.     ,17176
  112.     ,17177
  113.     ,17883
  114.     ,17884
  115.     ,18779
  116.     ,19030
  117.     ,19031
  118.     ,19032
  119.     ,19033
  120.     ,19034
  121.     ,20624
  122.     ,20625
  123.     ,21695
  124.     )
  125. go
  126.  
  127. insert into master..sysmessages (error, severity, dlevel, description, msglangid)
  128.     values
  129.     (211, 23, 1, N'Possible schema corruption. Run DBCC CHECKCATALOG.', 1033)
  130.  
  131. insert into master..sysmessages (error, severity, dlevel, description, msglangid)
  132.     values
  133.     (569, 16, 0, N'The handle passed to fn_get_sql was invalid.', 1033)
  134.  
  135. insert into master..sysmessages (error, severity, dlevel, description, msglangid)
  136.     values
  137.     (570, 16, 0, N'INSTEAD OF triggers do not support direct recursion. Trigger execution failed.', 1033)
  138.  
  139. insert into master..sysmessages (error, severity, dlevel, description, msglangid)
  140.     values
  141.     (1229, 10, 0, N'Process ID %d:%d owns resources that are blocking processes on Scheduler %d.', 1033)
  142.  
  143. insert into master..sysmessages (error, severity, dlevel, description, msglangid)
  144.     values
  145.     (1836, 10, 0, N'Could not create default data file because the name was too long.', 1033)
  146.  
  147. insert into master..sysmessages (error, severity, dlevel, description, msglangid)
  148.     values
  149.     (3124, 14, 0, N'Must be a System Administrator to perform LOAD with CONVERT65.', 1033)
  150.  
  151. insert into master..sysmessages (error, severity, dlevel, description, msglangid)
  152.     values
  153.     (4454, 16, 0, N'View ''%.*ls'' is not updatable because the %s statement contains a GROUP BY ALL clause.', 1033)
  154.  
  155. insert into master..sysmessages (error, severity, dlevel, description, msglangid)
  156.     values
  157.     (4455, 16, 0, N'Derived table ''%.*ls'' is not updatable because the %s statement contains a GROUP BY ALL clause.', 1033)
  158.  
  159. insert into master..sysmessages (error, severity, dlevel, description, msglangid)
  160.     values
  161.     (5600,16,0,'The Cross Database Chaining option cannot be set to the specified value on the specified database.', 1033)
  162.  
  163. insert into master..sysmessages (error, severity, dlevel, description, msglangid)
  164.     values
  165.     (14360, 16, 0, N'%s is already configured as TSX machine', 1033)
  166.  
  167. insert into master..sysmessages (error, severity, dlevel, description, msglangid)
  168.     values
  169.     (14361, 16, 0, N'MSX server does not support mixed security mode', 1033)
  170.  
  171. insert into master..sysmessages (error, severity, dlevel, description, msglangid)
  172.     values
  173.     (14362, 16, 0, N'The MSX server must be running the Standard or Enterprise edition of SQL Server', 1033)
  174.  
  175. insert into master..sysmessages (error, severity, dlevel, description, msglangid)
  176.     values
  177.     (14363, 16, 0, N'The MSX server is not prepared for enlistments [there must be an operator named ''MSXOperator'' defined at the MSX]', 1033)
  178.  
  179. insert into master..sysmessages (error, severity, dlevel, description, msglangid)
  180.     values
  181.     (14364, 16, 0, N'The TSX server is not currently enlisted', 1033)
  182.  
  183. insert into master..sysmessages (error, severity, dlevel, description, msglangid)
  184.     values
  185.     (17176, 10, 128, N'This instance of SQL Server last reported using a process id of %1 at %2 (local) %3 (UTC).', 1033)
  186.  
  187. insert into master..sysmessages (error, severity, dlevel, description, msglangid)
  188.     values
  189.     (17177, 10, 128, N'This instance of SQL Server has been using a process id of %1 since %2 (local) %3 (UTC).', 1033)
  190.  
  191. insert into master..sysmessages (error, severity, dlevel, description, msglangid)
  192.     values
  193.     (17883, 10, 128, N'The Scheduler %1!ld! appears to be hung. SPID %2!ld!, ECID %3!ld!, UMS Context 0x%4!p!', 1033)
  194.  
  195. insert into master..sysmessages (error, severity, dlevel, description, msglangid)
  196.     values
  197.     (17884, 10, 128, N'Potential deadlocks exist on all the schedulers', 1033)
  198.  
  199. insert into master..sysmessages (error, severity, dlevel, description, msglangid)
  200.     values
  201.     (18779, 16, 0, N'%s: Number of delete and insert log records mismatch in bounded update, insert:%d, delete:%d, beginLSN:(%ls), endLSN(%ls)', 1033)
  202.  
  203. insert into master..sysmessages (error, severity, dlevel, description, msglangid)
  204.     values
  205.     (19030, 10, 128, N'SQL Trace started.', 1033)
  206.  
  207. insert into master..sysmessages (error, severity, dlevel, description, msglangid)
  208.     values
  209.     (19031, 10, 128, N'SQL Trace stopped.', 1033)
  210.  
  211. insert into master..sysmessages (error, severity, dlevel, description, msglangid)
  212.     values
  213.     (19032, 10, 128, N'SQL Trace stopped due to server shutdown.', 1033)
  214.  
  215. insert into master..sysmessages (error, severity, dlevel, description, msglangid)
  216.     values
  217.     (19033, 10, 128, N'Server started with ''-f''. Auditing will not be started.', 1033)
  218.  
  219. insert into master..sysmessages (error, severity, dlevel, description, msglangid)
  220.     values
  221.     (19034, 16, 128, N'Cannot start C2 audit trace. SQL Server is shutting down.', 1033)
  222.  
  223. insert into master..sysmessages (error, severity, dlevel, description, msglangid)
  224.     values
  225.     (20624, 16, 0, N'Server user ''%s'' is not a valid user in database ''%s''. Add the user account into the database first.', 1033)
  226.  
  227. insert into master..sysmessages (error, severity, dlevel, description, msglangid)
  228.     values
  229.     (20625, 16, 0, N'Could not create the merge replication PAL database role for publication ''%s''.', 1033)
  230.  
  231. insert into master..sysmessages (error, severity, dlevel, description, msglangid)
  232.     values
  233.     (21695, 10, 0, N'The job name ''%s'' was not generated for this replication agent, delete the job manually when it is no longer in use.', 1033)
  234.  
  235. --------------------------------------------------------------------------------
  236. --    System Messages (MESSAGES.SQL / MSGWORK.SQL / SERVMSGS.SQL) Localized
  237. --------------------------------------------------------------------------------
  238.  
  239. --------------------------------------------------------------------------------
  240. --    Engine System Procs (U_TABLES.SQL / PROCSYST.SQL / ODSOLE.SQL)
  241. --------------------------------------------------------------------------------
  242.  
  243. if exists (select * from sysobjects
  244.        where xtype = N'TF' and uid = USER_ID('system_function_schema') and
  245.         name = 'fn_get_sql')
  246.     drop function system_function_schema.fn_get_sql
  247. go
  248.  
  249. raiserror(15339,-1,-1,'system_function_schema.fn_get_sql')
  250. go
  251. create function system_function_schema.fn_get_sql
  252.     (
  253.     @handle binary(20)
  254.     )
  255. returns @tab table(dbid SmallInt,
  256.     objectid Int,
  257.     number SmallInt,
  258.     encrypted Bit NOT NULL,
  259.     text Text)
  260. as
  261. begin
  262.     insert @tab
  263.     select * from OpenRowset(FnGetSql, @handle)
  264.  
  265.     return
  266. end -- fn_get_sql
  267. go
  268.  
  269. if object_id('sp_fulltext_catalog','P') IS NOT NULL
  270.     drop procedure sp_fulltext_catalog
  271.  
  272. ---------------------------- sp_fulltext_catalog ------------------------------
  273.  
  274. raiserror(15339,-1,-1,'sp_fulltext_catalog')
  275. go
  276. create proc sp_fulltext_catalog
  277.     @ftcat        sysname,        -- full-text catalog name
  278.     @action     varchar(20),    -- create | drop | | rebuild | ...
  279.     @path        nvarchar(101) = null    -- optional file path for create (max of 100 chars!!!)
  280. as
  281.     declare @objname sysname,
  282.             @objid    int,
  283.             @vc1    nvarchar(517),
  284.             @tabname    nvarchar(517),
  285.             @tabwarn int
  286.  
  287.     select @tabwarn = 0
  288.  
  289.     -- FULLTEXT MUST BE ACTIVE IN DATABASE --
  290.     if DatabaseProperty(db_name(), 'IsFulltextEnabled') = 0
  291.     begin
  292.         raiserror(15601,-1,-1)
  293.         return 1
  294.     end
  295.  
  296.     -- VALIDATE PARAMS --
  297.     if @action is null
  298.         OR @action not in ('create','drop','start_full','start_incremental','stop','rebuild')
  299.         OR @ftcat is null OR len(@ftcat) = 0
  300.         OR (@path is not null and @action <> 'create')
  301.         OR (len(@path) > 100 )
  302.     begin
  303.         raiserror(15600,-1,-1,'sp_fulltext_catalog')
  304.         return 1
  305.     end
  306.  
  307.     -- DISALLOW USER TRANSACTION --
  308.     set implicit_transactions off
  309.     if @@trancount > 0
  310.     begin
  311.         raiserror(15002,-1,-1,'sp_fulltext_catalog')
  312.         return 1
  313.     end
  314.  
  315.     -- CHECK PERMISSIONS (must be a dbowner) --
  316.     if (is_member('db_owner') = 0)
  317.     begin
  318.         raiserror(15247,-1,-1)
  319.         return 1
  320.     end
  321.  
  322.     -- CHECK DATABASE MODE (must not be read-only) --
  323.     if DATABASEPROPERTY(db_name(), 'IsReadOnly') = 1
  324.     begin
  325.         raiserror(15635, -1, -1, 'sp_fulltext_catalog')
  326.         return 1
  327.     end
  328.  
  329.     -- CATALOG MUST EXIST IF NOT CREATING --
  330.     declare @ftcatid smallint
  331.     select @ftcatid = ftcatid from sysfulltextcatalogs where name = @ftcat
  332.     if @action not in ('create', 'drop') and @ftcatid is null
  333.     begin
  334.         raiserror(7641,-1,-1,@ftcat)
  335.         return 1
  336.     end
  337.  
  338.     if @action = 'create'
  339.     begin
  340.         DBCC CALLFULLTEXT ( 1, @ftcat, @path )    -- FTCreateCatalog( @ftcat, @path )
  341.         if @@error <> 0 
  342.             return 1
  343.     end
  344.  
  345.     if @action = 'drop'
  346.     begin
  347.         -- CANNOT DROP CATALOG IF USED --
  348.         if exists (select * from sysobjects where ftcatid = @ftcatid)
  349.         begin
  350.             raiserror(15604,-1,-1, @ftcat)
  351.             return 1
  352.         end
  353.  
  354.         DBCC CALLFULLTEXT ( 2, @ftcat )    -- FTDropCatalog( @ftcat )
  355.         if @@error <> 0
  356.             return 1
  357.     end
  358.  
  359.     if @action = 'start_full'
  360.     begin
  361.         -- ERROR IF DATABASE IS IN SINGLE USER MODE --
  362.         if DATABASEPROPERTY(db_name(), 'IsSingleUser') = 1
  363.         begin
  364.             raiserror(15636, -1, -1, @ftcat)
  365.             return 1
  366.         end
  367.  
  368.         begin tran
  369.         -- MARK TABLES/URLs AS --
  370.         declare ms_crs_ftind cursor static local for select id, name from sysobjects
  371.                     where ftcatid = @ftcatid
  372.         open ms_crs_ftind
  373.         fetch ms_crs_ftind into @objid, @tabname
  374.         while @@fetch_status >= 0
  375.         begin
  376.  
  377.             -- ERROR ON TABLE IF TABLE IS NOT ACTIVATED --
  378.             if (ObjectProperty(@objid, 'TableHasActiveFulltextIndex') = 0)
  379.             begin
  380.                 raiserror(15630, -1, -1, @tabname)
  381.                 goto error_exit
  382.             end
  383.  
  384.             -- SKIP TABLE IF CRAWL ALREADY IN PROGRESS --
  385.             if (ObjectProperty(@objid, 'TableFulltextPopulateStatus') != 0)
  386.             begin
  387.                 select @tabwarn = 1
  388.                 fetch ms_crs_ftind into @objid, @tabname
  389.                 continue
  390.             end
  391.  
  392.  
  393.             -- START FULL CRAWL
  394.             DBCC CALLFULLTEXT ( 12, @ftcatid, @objid, 0 )
  395.             if @@error <> 0    -- server raised an error
  396.             begin
  397.                 -- server did an ex_raise - this is unreachable code
  398.                 goto error_exit
  399.             end
  400.  
  401.             -- DELETE NOTIFICATIONS FROM SYSFULLTEXTNOTIFY --
  402.             delete sysfulltextnotify where tableid = @objid
  403.  
  404.             if (ObjectProperty(@objid, 'TableIsFulltextSchemaModified') = 1)
  405.             and (ObjectProperty(@objid, 'TableFulltextChangeTrackingOn') = 0)
  406.             begin
  407.  
  408.                 select @vc1 = quotename(user_name(OBJECTPROPERTY(@objid,'OwnerId'))) + '.'
  409.                             + quotename(object_name(@objid))
  410.  
  411.                 dbcc lockobjectschema(@vc1)
  412.  
  413.                 update sysobjects set status = status & ~128 where id = @objid
  414.  
  415.             end
  416.  
  417.             fetch ms_crs_ftind into @objid, @tabname
  418.         end
  419.         deallocate ms_crs_ftind
  420.  
  421.         commit tran
  422.  
  423.     end
  424.  
  425.     if @action = 'start_incremental'
  426.     begin
  427.         -- ERROR IF DATABASE IS IN SINGLE USER MODE --
  428.         if DATABASEPROPERTY(db_name(), 'IsSingleUser') = 1
  429.         begin
  430.             raiserror(15636, -1, -1, @ftcat)
  431.             return 1
  432.         end
  433.         begin tran
  434.  
  435.         -- MARK TABLES/URLs AS --
  436.         declare ms_crs_ftind cursor static local for select id, name from sysobjects
  437.                     where ftcatid = @ftcatid
  438.         open ms_crs_ftind
  439.         fetch ms_crs_ftind into @objid, @tabname
  440.         while @@fetch_status >= 0
  441.         begin
  442.             -- ERROR ON TABLE IF TABLE IS NOT ACTIVATED --
  443.             if (ObjectProperty(@objid, 'TableHasActiveFulltextIndex') = 0)
  444.             begin
  445.                 raiserror(15630, -1, -1, @tabname)
  446.                 goto error_exit
  447.             end
  448.  
  449.             -- SKIP TABLE IF CRAWL ALREADY IN PROGRESS --
  450.             if (ObjectProperty(@objid, 'TableFulltextPopulateStatus') != 0)
  451.             begin
  452.                 select @tabwarn = 1
  453.                 fetch ms_crs_ftind into @objid, @tabname
  454.                 continue
  455.             end
  456.  
  457.             if (ObjectProperty(@objid, 'TableIsFulltextSchemaModified') = 1)
  458.             and (ObjectProperty(@objid, 'TableFulltextChangeTrackingOn') = 0)
  459.             begin
  460.  
  461.                 -- START A FULL POPULATION FOR THIS TABLE --
  462.                 DBCC CALLFULLTEXT ( 12, @ftcatid, @objid, 0 )
  463.                 if @@error <> 0
  464.                 begin
  465.                     -- server did an ex_raise - this is unreachable code --
  466.                     goto error_exit
  467.                 end
  468.  
  469.                 select @vc1 = quotename(user_name(OBJECTPROPERTY(@objid,'OwnerId'))) + '.'
  470.                         + quotename(object_name(@objid))
  471.  
  472.                 dbcc lockobjectschema(@vc1)
  473.  
  474.                 update sysobjects set status = status & ~128 where id = @objid
  475.  
  476.             end
  477.             else
  478.             begin
  479.                 -- START AN INCREMENTAL POPULATION FOR THIS TABLE --
  480.                 DBCC CALLFULLTEXT ( 12, @ftcatid, @objid, 1 )
  481.                 if @@error <> 0
  482.                 begin
  483.                     -- server did an ex_raise - this is unreachable code --
  484.                     goto error_exit
  485.                 end
  486.  
  487.             end
  488.  
  489.             -- DELETE NOTIFICATIONS FROM SYSFULLTEXTNOTIFY --
  490.             delete sysfulltextnotify where tableid = @objid
  491.  
  492.             fetch ms_crs_ftind into @objid, @tabname
  493.         end
  494.         deallocate ms_crs_ftind
  495.         commit tran
  496.  
  497.  
  498.     end
  499.  
  500.     if @action = 'stop'
  501.     begin
  502.         declare ms_crs_ftind cursor static local for select id, name from sysobjects
  503.                     where ftcatid = @ftcatid
  504.         open ms_crs_ftind
  505.         fetch ms_crs_ftind into @objid, @tabname
  506.         while @@fetch_status >= 0
  507.         begin
  508.  
  509.             -- ERROR ON TABLE IF TABLE IS NOT ACTIVATED --
  510.             if ObjectProperty(@objid, 'TableHasActiveFulltextIndex') = 0
  511.             begin
  512.                 raiserror(15630, -1, -1, @tabname)
  513.                 return 1
  514.             end
  515.  
  516.             -- SKIP TABLE IF CRAWL ALREADY STOPPED - NO WARNING --
  517.             if (ObjectProperty(@objid, 'TableFulltextPopulateStatus') = 0)
  518.             begin
  519.                 fetch ms_crs_ftind into @objid, @tabname
  520.                 continue
  521.             end
  522.  
  523.             -- ERROR IF POPULATE STATUS OF THE TABLE IS CRAWLING AND CT ON
  524.             if (ObjectProperty(@objid, 'TableFulltextChangeTrackingOn') = 1)
  525.             and ((ObjectProperty(@objid, 'TableFulltextPopulateStatus') = 1)
  526.             or (ObjectProperty(@objid, 'TableFulltextPopulateStatus') = 2))
  527.             begin
  528.                 raiserror(15642,-1,-1, @tabname)
  529.                 return 1
  530.             end
  531.  
  532.             -- STOP A FULL/INCREMENTAL POPULATION FOR THIS TABLE --
  533.             DBCC CALLFULLTEXT ( 12, @ftcatid, @objid, 2 )
  534.             if @@error <> 0
  535.             begin
  536.                 -- server did an ex_raise - this is unreachable code --
  537.                 return 1
  538.             end
  539.  
  540.             fetch ms_crs_ftind into @objid, @tabname
  541.         end
  542.         deallocate ms_crs_ftind
  543.  
  544.     end
  545.  
  546.     if @action = 'rebuild'
  547.     begin
  548.  
  549.         -- RE-CREATE CATALOG (Will first drop)
  550.         select @path = path from sysfulltextcatalogs where ftcatid = @ftcatid
  551.         DBCC CALLFULLTEXT ( 16, @ftcat, @path )    -- FTCreateCatalog( @ftcat, @path )
  552.         if @@error <> 0
  553.         begin 
  554.             -- server did an ex_raise - this is unreachable code --
  555.             return 1
  556.         end
  557.  
  558.         begin tran
  559.  
  560.         -- RE-ACTIVATE TABLES/URLs --
  561.         declare ms_crs_ftind cursor static local for select id from sysobjects
  562.                     where ftcatid = @ftcatid
  563.         open ms_crs_ftind
  564.         fetch ms_crs_ftind into @objid
  565.         while @@fetch_status >= 0
  566.         begin
  567.             DBCC CALLFULLTEXT ( 5, @ftcatid, @objid )    -- FTAddURL( @ftcat, db_id(), @objid )
  568.             if @@error <> 0
  569.             begin
  570.                 -- server did an ex_raise - this is unreachable code --
  571.                 goto error_exit
  572.             end
  573.  
  574.             -- CHECK TABLE FOR NOTIFICATIONS --
  575.             if ObjectProperty(@objid, 'TableFulltextChangeTrackingOn') = 1
  576.                 and ObjectProperty(@objid, 'TableHasActiveFulltextIndex') = 1
  577.             begin
  578.                 -- ERROR IF DATABASE IS IN SINGLE USER MODE --
  579.                 if DATABASEPROPERTY(db_name(), 'IsSingleUser') = 1
  580.                 begin
  581.                     select @objname = object_name(@objid)
  582.                     raiserror(15638, -1, -1, @objname)
  583.  
  584.                     select @vc1 = quotename(user_name(OBJECTPROPERTY(@objid,'OwnerId'))) + '.'
  585.                         + quotename(@objname)
  586.  
  587.  
  588.                     dbcc lockobjectschema(@vc1)
  589.  
  590.                     -- DISABLE FULLTEXT AUTO PROPAGATION (NO ERROR IF ALREADY DISABLED AND --
  591.                     -- IGNORE ANY OTHER ERRORS) --
  592.                     DBCC CALLFULLTEXT ( 9, @objid )    -- FTDisableNotify( db_id(), @objid )
  593.                     if @@error <> 0
  594.                     begin
  595.                         -- server did an ex_raise - this is unreachable code --
  596.                         goto error_exit
  597.                     end
  598.  
  599.                     -- TURN OFF CHANGE TRACKING ACTIVE BITS IN SYSOBJECTS --
  600.                     update sysobjects set status = status & ~192 where id = @objid
  601.  
  602.                     fetch ms_crs_ftind into @objid
  603.                     continue
  604.                 end
  605.  
  606.                 -- STOP A FULL/INCREMENTAL POPULATION FOR THIS TABLE --
  607.                 DBCC CALLFULLTEXT ( 12, @ftcatid, @objid, 2 )
  608.                 if @@error <> 0
  609.                 begin
  610.                     -- server did an ex_raise - this is unreachable code --
  611.                     goto error_exit
  612.                 end
  613.  
  614.                 -- START A FULL CRAWL FOR THIS TABLE --
  615.                 DBCC CALLFULLTEXT ( 12, @ftcatid, @objid, 0 )
  616.                 if @@error <> 0
  617.                 begin
  618.                     -- server did an ex_raise - this is unreachable code --
  619.                     goto error_exit
  620.                 end
  621.  
  622.                 -- DELETE NOTIFICATIONS FROM SYSFULLTEXTNOTIFY --
  623.                 delete sysfulltextnotify where tableid = @objid
  624.  
  625.             end
  626.  
  627.             -- CHECK TABLE FOR AUTOPROPAGATION  -
  628.             if ObjectProperty(@objid, 'TableFulltextAutoPropagationOn') = 1
  629.                 and ObjectProperty(@objid, 'TableHasActiveFulltextIndex') = 1
  630.             begin
  631.                 DBCC CALLFULLTEXT ( 10, @ftcatid, @objid )    -- FTEnableAutoProp( @ftcatid, db_id(), @objid )
  632.                 if @@error <> 0
  633.                 begin
  634.                     -- server did an ex_raise - this is unreachable code --
  635.                     goto error_exit
  636.                 end
  637.             end
  638.  
  639.             fetch ms_crs_ftind into @objid
  640.         end
  641.         deallocate ms_crs_ftind
  642.  
  643.         commit tran
  644.  
  645.     end
  646.     if(@tabwarn <> 0)
  647.     begin
  648.         raiserror(15643, -1, -1)
  649.         return 0
  650.     end
  651.     return 0    -- sp_fulltext_catalog
  652.  
  653. error_exit:
  654.     -- 'stop', 'rebuild' never get here, this is only for 'start_full', 'start_incr'
  655.     -- here we commit the changes for all tables on which the operation succeeded.  
  656.     -- Before 'goto error_exit' is called, schema changes made to table currently 
  657.     -- under cursor must be undone (so far, no schema changes)
  658.     commit tran
  659.     return 1    -- sp_fulltext_catalog
  660.  
  661. go
  662.  
  663. grant execute on sp_fulltext_catalog to public
  664. go
  665.  
  666.  
  667. --------------------------------------------------------------------------------
  668. -- sp_change_users_login
  669. --------------------------------------------------------------------------------
  670. if object_id('sp_change_users_login','P') IS NOT NULL
  671.     drop procedure sp_change_users_login
  672. go
  673.  
  674. raiserror(15339,-1,-1,'sp_change_users_login')
  675. go
  676. CREATE PROCEDURE sp_change_users_login
  677.     @Action               varchar(10)       -- REPORT / UPDATE_ONE / AUTO_FIX
  678.    ,@UserNamePattern      sysname  = Null
  679.    ,@LoginName            sysname  = Null
  680.    ,@Password              sysname  = Null
  681. AS
  682.     -- SETUP RUNTIME OPTIONS / DECLARE VARIABLES --
  683.     set nocount on
  684.     declare @exec_stmt nvarchar(430)
  685.  
  686.     declare @ret            int,
  687.             @FixMode        char(5),
  688.             @cfixesupdate   int,        -- count of fixes by update
  689.             @cfixesaddlogin int,        -- count of fixes by sp_addlogin
  690.             @dbname         sysname,
  691.             @loginsid       varbinary(85),
  692.             @110name        sysname
  693.  
  694.     -- SET INITIAL VALUES --
  695.     select  @dbname         = db_name(),
  696.             @cfixesupdate   = 0,
  697.             @cfixesaddlogin = 0
  698.  
  699.     -- ERROR IF IN USER TRANSACTION --
  700.     if @@trancount > 0
  701.     begin
  702.         raiserror(15289,-1,-1)
  703.         return (1)
  704.     end
  705.  
  706.     -- INVALIDATE USE OF SPECIAL LOGIN/USER NAMES --
  707.     if suser_sid(@LoginName) = 0x1    -- 'sa'
  708.     begin
  709.         raiserror(15287,-1,-1,@LoginName)
  710.         return (1)
  711.     end
  712.     if user_id(@UserNamePattern) in (1,0,3,4) --dbo, public, INFORMATION_SCHEMA, system_function_schema
  713.     begin
  714.         raiserror(15287,-1,-1,@UserNamePattern)
  715.         return (1)
  716.     end
  717.  
  718.     -- HANDLE REPORT --
  719.     if upper(@Action) = 'REPORT'
  720.     begin
  721.  
  722.         -- VALIDATE PARAMS --
  723.         if @UserNamePattern IS NOT Null or @LoginName IS NOT Null
  724.         begin
  725.             raiserror(15290,-1,-1,@Action,@UserNamePattern,@LoginName)
  726.             return (1)
  727.         end
  728.  
  729.         -- GENERATE REPORT --
  730.         select UserName = name, UserSID = sid from sysusers
  731.             where issqluser = 1 and (sid is not null and sid <> 0x0)
  732.                     and suser_sname(sid) is null
  733.             order by name
  734.         return (0)
  735.     end
  736.  
  737.     -- HANDLE UPDATE_ONE --
  738.     if upper(@Action) = 'UPDATE_ONE'
  739.     begin
  740.  
  741.         -- CHECK PERMISSIONS --
  742.         if not is_member('db_owner') = 1
  743.         begin
  744.             raiserror(15247,-1,-1)
  745.             return (1)
  746.         end
  747.  
  748.         -- ERROR IF PARAMS NULL --
  749.         if @UserNamePattern IS Null or @LoginName IS Null
  750.         begin
  751.             raiserror(15290,-1,-1,@Action,@UserNamePattern,@LoginName)
  752.             return (1)
  753.         end
  754.  
  755.         -- VALIDATE PARAMS --
  756.         -- Can ONLY remap SQL Users to SQL Logins!  Should be no need
  757.         --  for re-mapping NT logins, and if you try, you'll mess up
  758.         --  the user status bits! 
  759.         if not exists (select name from sysusers where
  760.                 name = @UserNamePattern             -- match user name
  761.             and issqluser = 1)                      -- must be sql user
  762.         begin
  763.             raiserror(15291,-1,-1,'User',@UserNamePattern)
  764.             return (1)
  765.         end
  766.         select @loginsid = sid from master.dbo.syslogins where
  767.                 loginname = @LoginName              -- match login name
  768.             and isntname = 0                        -- cannot use nt logins
  769.         if @loginsid is null
  770.         begin
  771.             raiserror(15291,-1,-1,'Login',@LoginName)
  772.             return (1)
  773.         end
  774.  
  775.         -- ERROR IF SID ALREADY IN USE IN DATABASE --
  776.         if exists (select sid from sysusers where sid = @loginsid
  777.                     and name <> @UserNamePattern)
  778.         begin
  779.             raiserror(15063,-1,-1)
  780.             return (1)
  781.         end
  782.  
  783.         -- CHANGE THE USERS LOGIN (SID) --
  784.         update sysusers set sid = @loginsid, updatedate = getdate()
  785.                 where name = @UserNamePattern and issqluser = 1
  786.                 and sid <> @loginsid
  787.  
  788.         -- FINALIZATION: REPORT (ONLY IF NOT SUCCESSFUL) AND EXIT --
  789.         if @@error <> 0 or @@rowcount <> 1
  790.             raiserror(15295,-1,-1, 0)
  791.         return (0)
  792.     end
  793.  
  794.     -- ERROR IF NOT AUTO_FIX --
  795.     if upper(@Action) <> 'AUTO_FIX'
  796.     begin
  797.         raiserror(15286,-1,-1,@Action)
  798.         return (1)
  799.     end
  800.  
  801.     -- HANDLE AUTO_FIX --
  802.     -- CHECK PERMISSIONS --
  803.     if not is_srvrolemember('sysadmin') = 1
  804.     begin
  805.         raiserror(15247,-1,-1)
  806.         return (1)
  807.     end
  808.  
  809.     -- VALIDATE PARAMS --
  810.     if @UserNamePattern IS Null or @LoginName IS NOT Null
  811.     begin
  812.         raiserror(15290,-1,-1,@Action,@UserNamePattern,@LoginName)
  813.         return (1)
  814.     end
  815.  
  816.     -- LOOP THRU ORPHANED USERS --
  817.     select @exec_stmt = 'DECLARE ms_crs_110_Users cursor global for
  818.             select name from sysusers
  819.             where name = N' + quotename( @UserNamePattern , '''')+ '
  820.                 and issqluser = 1 and suser_sname(sid) is null'
  821.     EXECUTE (@exec_stmt)
  822.     OPEN ms_crs_110_Users
  823.  
  824.     WHILE (110=110)
  825.     begin
  826.         FETCH next from ms_crs_110_Users into @110name
  827.         if (@@fetch_status <> 0)
  828.         begin
  829.             DEALLOCATE ms_crs_110_Users
  830.             BREAK
  831.         end
  832.  
  833.         -- IS NAME ALREADY IN USE? --
  834.         -- if suser_sid(@110name) is null
  835.         if not exists(select * from master.dbo.syslogins where loginname = @110name)
  836.         begin
  837.  
  838.            -- VALIDATE PARAMS --
  839.             if @Password IS Null
  840.             begin
  841.                 raiserror(15290,-1,-1,@Action,@UserNamePattern,@LoginName)
  842.                 return (1)
  843.             end
  844.  
  845.             -- ADD LOGIN --
  846.             execute @ret = sp_addlogin @110name, @Password, @dbname
  847.             if @ret <> 0 or suser_sid(@110name) is null
  848.             begin
  849.                 raiserror(15497,16,1,@110name)
  850.                 deallocate ms_crs_110_Users
  851.                 return (1)
  852.             end
  853.             select @FixMode = '1AddL'
  854.             raiserror(15293,-1,-1,@110name)
  855.         end
  856.         ELSE
  857.         begin
  858.             Select @FixMode = '2UpdU'
  859.             Raiserror(15292,-1,-1,@110name)
  860.         end
  861.  
  862.         -- REPORT ERROR & CONTINUE IF DUPLICATE SID IN DB --
  863.         select @loginsid = suser_sid(@110name)
  864.         if user_sid(@loginsid) is not null
  865.         begin
  866.             raiserror(15331,-1,-1,@110name)
  867.             CONTINUE
  868.         end
  869.  
  870.         -- UPDATE SYSUSERS ROW --
  871.         update sysusers set sid = @loginsid, updatedate = getdate(), status = (status & ~1) | 2 where name = @110name
  872.         if @@error <> 0
  873.         begin
  874.             raiserror(15498,17,127)
  875.             deallocate ms_crs_110_Users
  876.             return (1)
  877.         end
  878.  
  879.  
  880.         if @FixMode = '1AddL'
  881.             Select @cfixesaddlogin = @cfixesaddlogin + 1
  882.         else
  883.             Select @cfixesupdate = @cfixesupdate + 1
  884.     end -- loop 110
  885.  
  886.     -- REPORT AND RETURN SUCCESS --
  887.     raiserror(15295,-1,-1,@cfixesupdate)
  888.     raiserror(15294,-1,-1,@cfixesaddlogin)
  889.     return (0) -- sp_change_users_login
  890. go
  891.  
  892.  
  893. grant execute on sp_change_users_login to public
  894. go
  895. --------------------------------------------------------------------------------
  896.  
  897. ------------------------------ sp_changedbowner -------------------------------
  898. if object_id('sp_changedbowner','P') IS NOT NULL
  899.     drop procedure sp_changedbowner
  900. go
  901.  
  902. raiserror(15339,-1,-1,'sp_changedbowner')
  903. go
  904. create procedure sp_changedbowner
  905.     @loginame       sysname,        -- login to become dbo
  906.     @map            varchar(5) = NULL    -- True to map aliases, else drop
  907. as
  908.     -- SETUP RUNTIME OPTIONS / DECLARE VARIABLES --
  909.     set nocount on
  910.     declare @ret        int,
  911.             @newsid     varbinary(85),
  912.             @status     smallint
  913.  
  914.     -- CHECK PERMISSIONS (Note: All sysadmins are dbo) --
  915.     -- See Bug Shiloh 362190 ---
  916.     if not (is_srvrolemember('sysadmin') = 1)
  917.     begin
  918.         raiserror(15247,-1,-1)
  919.         return(1)
  920.     end
  921.  
  922.     -- CANT CHANGE OWNER OF MASTER/MODEL/TEMPDB --
  923.     if db_name() in ('master', 'model', 'tempdb')
  924.     begin
  925.         raiserror(15109,-1,-1)
  926.         return(1)
  927.     end
  928.  
  929.     -- CHECK LOGIN NAME IS VALID (NT/SQL USER ONLY!) --
  930.     select @newsid = sid, @status = 2 from master.dbo.syslogins
  931.                     where loginname = @loginame and isntname = 0
  932.     if @newsid is null
  933.         select @status = 14, @newsid = get_sid('\U'+@loginame, NULL)
  934.     if @newsid is null
  935.     begin
  936.         raiserror(15007,-1,-1,@loginame)
  937.         return (1)
  938.     end
  939.  
  940.     -- CHECK IF LOGIN ALREADY ALIASED IN DB --
  941.     if exists (select sid from sysusers where isaliased = 1 and sid = @newsid)
  942.     begin
  943.         raiserror(15111,-1,-1)
  944.         return (1)
  945.     end
  946.  
  947.     -- CHECK IF LOGIN ALREADY KNOWN TO DATABASE --
  948.     if exists (select sid from sysusers where sid = @newsid and uid <> 1)
  949.     begin
  950.         raiserror(15110,-1,-1)
  951.         return (1)
  952.     end
  953.  
  954.     -- MAKE THE FOLLOWING REMOVE/REMAP/DELETES ATOMIC --
  955.     begin transaction
  956.  
  957.     -- REMAP DBO TO NEW SID --
  958.     update sysusers set sid = @newsid, status = @status, updatedate = getdate()
  959.             where name = 'dbo'
  960.  
  961.     -- REMOVE OTHER DBO-ALIASES IF REMAPPING NOT REQUESTED --
  962.     if lower(@map) <> 'true'
  963.     begin
  964.         delete from sysusers where isaliased = 1 and altuid = user_id('dbo')
  965.         raiserror(15500,-1,-1)
  966.     end
  967.     else
  968.         raiserror(15499,-1,-1)     -- nothing to do to <remap>
  969.  
  970.     -- REFLECT NEW OWNER IN SYSDATABASES --
  971.     update master.dbo.sysdatabases set sid = @newsid where dbid = db_id()
  972.     commit transaction
  973.  
  974.     -- CHECKPOINT DATABASE TO FORCE CHANGES TO IN-MEMORY STRUCTURE --
  975.     checkpoint
  976.     raiserror(15501,-1,-1)
  977.     grant all to null
  978.     return (0) -- sp_changedbowner
  979. go
  980.  
  981. grant execute on sp_changedbowner to public
  982. go
  983. --------------------------------------------------------------------------
  984.  
  985. delete from spt_values where name = 'CrossDbOwnChain' and number = 158 and type = 'T'
  986. go
  987. insert spt_values(name, number, type)
  988.   values ('CrossDbOwnChain', 158, 'T')
  989. go
  990.  
  991. delete from spt_values where name = 'Cross DB Ownership Chaining' and number = 400 and type = 'C'
  992. go
  993. insert spt_values(name, number, type, low, high,status)
  994.     values ('Cross DB Ownership Chaining', 400, 'C', 0, 1, 0)
  995. go
  996.  
  997. delete from spt_values where name = 'db chaining' and number = 0x400 and type = 'D2'
  998. go
  999. insert spt_values (name, number, type)
  1000.     values ('db chaining', 0x400, 'D2')
  1001. go
  1002.  
  1003. delete from spt_values where name = 'ALL SETTABLE OPTIONS' and type = 'D2'
  1004. go
  1005. insert spt_values (name, number, type)
  1006.     values ('ALL SETTABLE OPTIONS', 1469267968|0x800|0x1000|0x2000|0x400, 'D2')
  1007. go
  1008.  
  1009. delete from spt_values where name = 'ALL SETTABLE OPTIONS' and type = 'D'
  1010. go
  1011. insert spt_values (name, number, type)
  1012.     values ('ALL SETTABLE OPTIONS', 4202013, 'D')
  1013. go
  1014.  
  1015. ------------------------------ sp_dboption -------------------------------
  1016. if object_id('sp_dboption','P') IS NOT NULL
  1017.     drop procedure sp_dboption
  1018. go
  1019.  
  1020. raiserror(15339,-1,-1,'sp_dboption')
  1021. go
  1022. /*ANSI_NULLS ON  for creation of sp_dboption*/
  1023. set ansi_nulls on
  1024. go
  1025. create procedure sp_dboption  -- 1999/08/09 18:25
  1026. @dbname sysname = NULL,     /* database name to change */
  1027. @optname varchar(35) = NULL,  /* option name to turn on/off */
  1028. @optvalue varchar(10) = NULL  /* true or false */
  1029. as
  1030.  
  1031. set nocount    on
  1032.  
  1033. declare @dbid int         /* dbid of the database */
  1034. declare @catvalue int     /* number of category option */
  1035. declare @optcount int      /* number of options like @optname */
  1036. declare @allstatopts int    /* bit map off all options stored in sysdatqabases.status
  1037.                             ** that can be set by sp_dboption. */
  1038. declare @alloptopts int    /* bit map off all options stored in sysdatqabases.status
  1039.                             ** that can be set by sp_dboption. */
  1040. declare @allcatopts int    /* bit map off all options stored in sysdatqabases.category
  1041.                             ** that can be set by sp_dboption. */
  1042. declare @exec_stmt nvarchar(550)
  1043. declare @fulloptname varchar(35)
  1044. declare @alt_optname varchar(50)
  1045. declare @alt_optvalue varchar(30)
  1046.  
  1047. declare @status int
  1048.  
  1049. /*
  1050. **  If no @dbname given, just list the possible dboptions.
  1051. **  Only certain status bits may be set or cleared by sp_dboption.
  1052. */
  1053.  
  1054. /*
  1055. ** Get bitmap of all options that can be set by sp_dboption.
  1056. */
  1057. select @allstatopts=number from master.dbo.spt_values where type = 'D'
  1058.    and name = 'ALL SETTABLE OPTIONS'
  1059.  
  1060. select @allcatopts=number from master.dbo.spt_values where type = 'DC'
  1061.    and name = 'ALL SETTABLE OPTIONS'
  1062.  
  1063. select @alloptopts=number from master.dbo.spt_values where type = 'D2'
  1064.    and name = 'ALL SETTABLE OPTIONS'
  1065.  
  1066. if @dbname is null
  1067. begin
  1068.    select 'Settable database options:' = name
  1069.       from master.dbo.spt_values
  1070.       where (type = 'D'
  1071.             and number & @allstatopts <> 0
  1072.             and number not in (0,@allstatopts))  /* Eliminate non-option entries */
  1073.          or (type = 'DC'
  1074.             and number & @allcatopts <> 0
  1075.             and number not in (0,@allcatopts))
  1076.          or (type = 'D2'
  1077.             and number & @alloptopts <> 0
  1078.             and number not in (0,@alloptopts))
  1079.       order by name
  1080.    return (0)
  1081. end
  1082.  
  1083. /*
  1084. **  Verify the database name and get info
  1085. */
  1086. select @dbid = dbid
  1087. from master.dbo.sysdatabases
  1088. where name = @dbname
  1089.  
  1090. /*
  1091. **  If @dbname not found, say so and list the databases.
  1092. */
  1093. if @dbid is null
  1094.    begin
  1095.       raiserror(15010,-1,-1,@dbname)
  1096.       print ' '
  1097.       select 'Available databases:' = name
  1098.          from master.dbo.sysdatabases
  1099.       return (1)
  1100.    end
  1101.  
  1102. /*
  1103. ** If no option was supplied, display current settings.
  1104. */
  1105. if @optname is null
  1106.    begin
  1107.       select 'The following options are set:' = v.name
  1108.          from master.dbo.spt_values v, master.dbo.sysdatabases d
  1109.             where d.name=@dbname
  1110.                and ((number & @allstatopts <> 0
  1111.                      and number not in (-1,@allstatopts)
  1112.                      and v.type = 'D'
  1113.                      and (v.number & d.status)=v.number)
  1114.                  or (number & @allcatopts <> 0
  1115.                      and number not in (-1,@allcatopts)
  1116.                      and v.type = 'DC'
  1117.                      and d.category & v.number <> 0)
  1118.                  or (number & @alloptopts <> 0
  1119.                      and number not in (-1,@alloptopts)
  1120.                      and v.type = 'D2'
  1121.                      and d.status2 & v.number <> 0))
  1122.       return(0)
  1123.    end
  1124.  
  1125.  
  1126. if lower(@optvalue) not in ('true', 'false', 'on', 'off') and @optvalue is not null
  1127.    begin
  1128.       raiserror(15241,-1,-1)
  1129.       return (1)
  1130.    end
  1131.  
  1132. /*
  1133. **  Use @optname and try to find the right option.
  1134. **  If there isn't just one, print appropriate diagnostics and return.
  1135. */
  1136. select @optcount = count(*) ,@fulloptname = min(name)
  1137.       from master.dbo.spt_values
  1138.       where lower(name) like '%' + lower(@optname) + '%'
  1139.          and ((type = 'D'
  1140.               and number & @allstatopts <> 0
  1141.               and number not in (-1,@allstatopts))
  1142.           or (type = 'DC'
  1143.               and number & @allcatopts <> 0
  1144.               and number not in (-1,@allcatopts))
  1145.             or (type = 'D2'
  1146.               and number & @alloptopts <> 0
  1147.               and number not in (-1,@alloptopts)))
  1148.  
  1149. /*
  1150. **  If no option, show the user what the options are.
  1151. */
  1152. if @optcount = 0
  1153.    begin
  1154.       raiserror(15011,-1,-1,@optname)
  1155.       print ' '
  1156.  
  1157.       select 'Settable database options:' = name
  1158.          from master.dbo.spt_values
  1159.          where (type = 'D'
  1160.                and number & @allstatopts <> 0
  1161.                and number not in (-1,@allstatopts))  /* Eliminate non-option entries */
  1162.             or (type = 'DC'
  1163.                and number & @allcatopts <> 0
  1164.                and number not in (-1,@allcatopts))
  1165.             or (type = 'D2'
  1166.                and number & @alloptopts <> 0
  1167.                and number not in (-1,@alloptopts))
  1168.          order by name
  1169.  
  1170.       return (1)
  1171.    end
  1172.  
  1173.  
  1174. /*
  1175. **  If more than one option like @optname, show the duplicates and return.
  1176. */
  1177. if @optcount > 1
  1178.    begin
  1179.       raiserror(15242,-1,-1,@optname)
  1180.       print ' '
  1181.  
  1182.       select duplicate_options = name
  1183.          from master.dbo.spt_values
  1184.          where lower(name) like '%' + lower(@optname) + '%'
  1185.             and ((type = 'D'
  1186.                  and number & @allstatopts <> 0
  1187.                  and number not in (-1,@allstatopts))
  1188.               or (type = 'DC'
  1189.                  and number & @allcatopts <> 0
  1190.                  and number not in (-1,@allcatopts))
  1191.               or (type = 'D2'
  1192.                  and number & @alloptopts <> 0
  1193.                  and number not in (-1,@alloptopts))
  1194.                 )
  1195.       return (1)
  1196.    end
  1197.  
  1198.  
  1199. /*
  1200. **  Just want to see current setting of specified option.
  1201. */
  1202. if @optvalue is null
  1203. begin
  1204.       select OptionName = v.name
  1205.  
  1206.             ,CurrentSetting =
  1207.                CASE
  1208.                   When ( ((v.number & d.status) = v.number
  1209.                           and v.type = 'D')
  1210.                       or (d.category & v.number <> 0
  1211.                            and v.type = 'DC')
  1212.                       or (d.status2 & v.number <> 0
  1213.                            and v.type = 'D2')
  1214.                        )
  1215.                      Then 'ON'
  1216.                   When NOT
  1217.                        ( ((v.number & d.status) = v.number
  1218.                           and v.type = 'D')
  1219.                       or (d.category & v.number <> 0
  1220.                            and v.type = 'DC')
  1221.                       or (d.status2 & v.number <> 0
  1222.                            and v.type = 'D2')
  1223.                        )
  1224.                      Then 'off'
  1225.                END
  1226.  
  1227.          from master.dbo.spt_values v, master.dbo.sysdatabases d
  1228.             where d.name=@dbname
  1229.                and ((v.number & @allstatopts <> 0
  1230.                      and v.number not in (-1,@allstatopts)   /* Eliminate non-option entries */
  1231.                      and v.type = 'D')
  1232.                  or (v.number & @allcatopts <> 0
  1233.                      and v.number not in (-1,@allcatopts)   /* Eliminate non-option entries */
  1234.                      and v.type = 'DC')
  1235.                  or (v.number & @alloptopts <> 0
  1236.                      and v.number not in (-1,@alloptopts)   /* Eliminate non-option entries */
  1237.                      and v.type = 'D2')
  1238.                    )
  1239.                 and lower(v.name) = lower(@fulloptname)
  1240.  
  1241.    return (0)
  1242. end
  1243.  
  1244.  
  1245. select @catvalue = 0
  1246. select @catvalue = number
  1247.       from master.dbo.spt_values
  1248.       where lower(name) = lower(@fulloptname)
  1249.       and type = 'DC'
  1250.  
  1251. /* if setting replication option, call sp_replicationdboption directly */
  1252. if (@catvalue <> 0)
  1253.     begin
  1254.         if lower(@optvalue) in ('true', 'on')
  1255.             begin
  1256.                 select @alt_optvalue = 'true'
  1257.             end
  1258.         else
  1259.             begin
  1260.                 select @alt_optvalue = 'false'
  1261.             end
  1262.  
  1263.         select alt_optname = quotename(@fulloptname, '''')
  1264.         select @exec_stmt = quotename(@dbname, '[')   + '.dbo.sp_replicationdboption'
  1265.  
  1266.         if @catvalue = 1
  1267.             begin
  1268.                 select @alt_optname  = 'publish'
  1269.             end
  1270.         if @catvalue = 2
  1271.             begin
  1272.                 select @alt_optname  = 'subscribe'
  1273.             end
  1274.         if @catvalue = 4
  1275.             begin
  1276.                 select @alt_optname  = 'merge publish'
  1277.             end
  1278.  
  1279.         exec @exec_stmt @dbname, @alt_optname, @alt_optvalue
  1280.         return (0)
  1281.     end
  1282.  
  1283.  
  1284. /* call Alter Database to set options */
  1285.  
  1286. /* set option value in alter database*/
  1287. if lower(@optvalue) in ('true', 'on')
  1288.    begin
  1289.         select @alt_optvalue = 'ON'
  1290.    end
  1291.  
  1292. else
  1293.     begin
  1294.         select @alt_optvalue = 'OFF'
  1295.     end
  1296.  
  1297. /* if Cross DB Ownership Chaining is the option. Set it and get out */
  1298. if lower(@fulloptname) = 'db chaining'
  1299.     begin
  1300.     
  1301.     -- CHECK PERMISSIONS (Note: All sysadmins are dbo) --
  1302.     if not (is_srvrolemember('sysadmin') = 1)
  1303.     begin
  1304.         raiserror(15247,-1,-1)
  1305.         return(1)
  1306.     end
  1307.  
  1308.     -- CANT SET IN ANY OF OF MASTER/MODEL/TEMPDB --
  1309.     if lower(@dbname) in ('master', 'model', 'tempdb') or
  1310.        (lower(@dbname) ='msdb' and @alt_optvalue = 'OFF')
  1311.     begin
  1312.         raiserror(5600,-1,-1)
  1313.         return(1)
  1314.     end
  1315.  
  1316.     -- ERROR IF IN USER TRANSACTION --
  1317.     if @@trancount > 0
  1318.     begin
  1319.         raiserror(15289,-1,-1)
  1320.         return (1)
  1321.     end
  1322.  
  1323.    -- MAKE THE FOLLOWING REMOVE/REMAP/DELETES ATOMIC --
  1324.     begin transaction
  1325.  
  1326.     select @status = status2 from master.dbo.sysdatabases where name = @dbname
  1327.     if @alt_optvalue = 'ON'
  1328.         set @status = @status | 1024
  1329.     else
  1330.         select @status = @status & (~ 1024)
  1331.     update master.dbo.sysdatabases set status2 = @status where name = @dbname
  1332.  
  1333.  
  1334.     -- REFLECT NEW STATUS IN SYSDATABASES --
  1335.     commit transaction
  1336.  
  1337.     -- CHECKPOINT DATABASE TO FORCE CHANGES TO IN-MEMORY STRUCTURE --
  1338.     select @exec_stmt = 'use ' +  quotename(@dbname, '[')   + ' checkpoint'         
  1339.     exec (@exec_stmt)        
  1340.  
  1341.     return (0) -- end of set db chaining/user info in doubt  option
  1342.     end
  1343.  
  1344.  
  1345. /* set option name in alter database */
  1346. if lower(@fulloptname) = 'auto create statistics'
  1347.     begin
  1348.         select @alt_optname = 'AUTO_CREATE_STATISTICS'
  1349.     end
  1350.  
  1351. if lower(@fulloptname) = 'auto update statistics'
  1352.     begin
  1353.         select @alt_optname = 'AUTO_UPDATE_STATISTICS'
  1354.     end
  1355.  
  1356. if lower(@fulloptname) = 'autoclose'
  1357.     begin
  1358.         select @alt_optname = 'AUTO_CLOSE'
  1359.     end
  1360.  
  1361. if lower(@fulloptname) = 'autoshrink'
  1362.     begin
  1363.         select @alt_optname = 'AUTO_SHRINK'
  1364.     end
  1365.  
  1366. if lower(@fulloptname) = 'ansi padding'
  1367.     begin
  1368.         select @alt_optname = 'ANSI_PADDING'
  1369.     end
  1370.  
  1371. if lower(@fulloptname) = 'arithabort'
  1372.     begin
  1373.         select @alt_optname = 'ARITHABORT'
  1374.     end
  1375.  
  1376. if lower(@fulloptname) = 'numeric roundabort'
  1377.     begin
  1378.         select @alt_optname = 'NUMERIC_ROUNDABORT'
  1379.     end
  1380.  
  1381. if lower(@fulloptname) = 'ansi null default'
  1382.     begin
  1383.         select @alt_optname = 'ANSI_NULL_DEFAULT'
  1384.     end
  1385.  
  1386. if lower(@fulloptname) = 'ansi nulls'
  1387.     begin
  1388.         select @alt_optname = 'ANSI_NULLS'
  1389.     end
  1390.  
  1391. if lower(@fulloptname) = 'ansi warnings'
  1392.     begin
  1393.         select @alt_optname = 'ANSI_WARNINGS'
  1394.     end
  1395.  
  1396. if lower(@fulloptname) = 'concat null yields null'
  1397.     begin
  1398.         select @alt_optname = 'CONCAT_NULL_YIELDS_NULL'
  1399.     end
  1400.  
  1401. if lower(@fulloptname) = 'cursor close on commit'
  1402.     begin
  1403.         select @alt_optname = 'CURSOR_CLOSE_ON_COMMIT'
  1404.     end
  1405.  
  1406. if lower(@fulloptname) = 'torn page detection'
  1407.     begin
  1408.         select @alt_optname = 'TORN_PAGE_DETECTION'
  1409.     end
  1410.  
  1411. if lower(@fulloptname) = 'quoted identifier'
  1412.     begin
  1413.         select @alt_optname = 'QUOTED_IDENTIFIER'
  1414.     end
  1415.  
  1416. if lower(@fulloptname) = 'recursive triggers'
  1417.     begin
  1418.         select @alt_optname = 'RECURSIVE_TRIGGERS'
  1419.     end
  1420.  
  1421. if lower(@fulloptname) = 'default to local cursor'
  1422.     begin
  1423.         select @alt_optname = 'CURSOR_DEFAULT'
  1424.  
  1425.         if @alt_optvalue = 'ON'
  1426.            begin
  1427.                 select @alt_optvalue = 'LOCAL'
  1428.            end
  1429.  
  1430.         else
  1431.             begin
  1432.                 select @alt_optvalue = 'GLOBAL'
  1433.             end
  1434.     end
  1435.  
  1436. if lower(@fulloptname) = 'offline'
  1437.     begin
  1438.         if @alt_optvalue = 'ON'
  1439.            begin
  1440.                 select @alt_optname = 'OFFLINE'
  1441.            end
  1442.  
  1443.         else
  1444.             begin
  1445.                 select @alt_optname = 'ONLINE'
  1446.             end
  1447.         select @alt_optvalue = ''
  1448.     end
  1449.  
  1450. if lower(@fulloptname) = 'read only'
  1451.     begin
  1452.         if @alt_optvalue = 'ON'
  1453.            begin
  1454.                 select @alt_optname = 'READ_ONLY'
  1455.            end
  1456.  
  1457.         else
  1458.             begin
  1459.                 select @alt_optname = 'READ_WRITE'
  1460.             end
  1461.         select @alt_optvalue = ''
  1462.     end
  1463.  
  1464. if lower(@fulloptname) = 'dbo use only'
  1465.     begin
  1466.         if @alt_optvalue = 'ON'
  1467.             begin
  1468.                 if databaseproperty(@dbname, 'IsSingleUser') = 1
  1469.                     begin
  1470.                         raiserror(5066,-1,-1);
  1471.                         return (1)
  1472.                     end
  1473.                 select @alt_optname = 'RESTRICTED_USER'
  1474.             end
  1475.  
  1476.         else
  1477.             begin
  1478.                 if databaseproperty(@dbname, 'IsDBOOnly') = 0
  1479.                     begin
  1480.                         return (0)
  1481.                     end
  1482.                 select @alt_optname = 'MULTI_USER'
  1483.             end
  1484.  
  1485.         select @alt_optvalue = ''
  1486.     end
  1487.  
  1488. if lower(@fulloptname) = 'single user'
  1489.     begin
  1490.         if @alt_optvalue = 'ON'
  1491.            begin
  1492.                 if databaseproperty(@dbname, 'ISDBOOnly') = 1
  1493.                     begin
  1494.                         raiserror(5066,-1,-1);
  1495.                         return (1)
  1496.                     end
  1497.                 select @alt_optname = 'SINGLE_USER'
  1498.            end
  1499.  
  1500.         else
  1501.             begin
  1502.                 if databaseproperty(@dbname, 'IsSingleUser') = 0
  1503.                     begin
  1504.                         return (0)
  1505.                     end
  1506.                 select @alt_optname = 'MULTI_USER'
  1507.             end
  1508.         select @alt_optvalue = ''
  1509.     end
  1510.  
  1511. if lower(@fulloptname) = 'select into/bulkcopy'
  1512.     begin
  1513.         select @alt_optname = 'RECOVERY'
  1514.  
  1515.         if @alt_optvalue = 'ON'
  1516.            begin
  1517.                 if databaseproperty(@dbname, 'IsTrunclog') = 1
  1518.                     begin
  1519.                         select @alt_optvalue = 'RECMODEL_70BACKCOMP'
  1520.                     end
  1521.                 else
  1522.                     begin
  1523.                         select @alt_optvalue = 'BULK_LOGGED'
  1524.                     end
  1525.            end
  1526.  
  1527.         else
  1528.             begin
  1529.                 if databaseproperty(@dbname, 'IsTrunclog') = 1
  1530.                     begin
  1531.                         select @alt_optvalue = 'SIMPLE'
  1532.                     end
  1533.                 else
  1534.                     begin
  1535.                         select @alt_optvalue = 'FULL'
  1536.                     end
  1537.             end
  1538.     end
  1539.  
  1540. if lower(@fulloptname) = 'trunc. log on chkpt.'
  1541.     begin
  1542.         select @alt_optname = 'RECOVERY'
  1543.  
  1544.         if @alt_optvalue = 'ON'
  1545.             begin
  1546.                 if databaseproperty(@dbname, 'IsBulkCopy') = 1
  1547.                     begin
  1548.                         select @alt_optvalue = 'RECMODEL_70BACKCOMP'
  1549.                     end
  1550.                 else
  1551.                     begin
  1552.                         select @alt_optvalue = 'SIMPLE'
  1553.                     end
  1554.             end
  1555.  
  1556.         else
  1557.             begin
  1558.                 if databaseproperty(@dbname, 'IsBulkCopy') = 1
  1559.                     begin
  1560.                         select @alt_optvalue = 'BULK_LOGGED'
  1561.                     end
  1562.                 else
  1563.                     begin
  1564.                         select @alt_optvalue = 'FULL'
  1565.                     end
  1566.             end
  1567.     end
  1568.  
  1569. /* construct the ALTER DATABASE command string */
  1570. select @exec_stmt = 'ALTER DATABASE ' + quotename(@dbname) +' SET ' + @alt_optname + ' ' + @alt_optvalue + ' WITH NO_WAIT'
  1571. exec (@exec_stmt)
  1572.  
  1573. if @@error <> 0
  1574.     begin
  1575.         raiserror(15627,-1,-1)
  1576.         return (1)
  1577.     end
  1578. else
  1579.     begin
  1580.         return (0)
  1581.     end
  1582.  
  1583. return (0) -- sp_dboption
  1584. go
  1585. set ansi_nulls off
  1586. go
  1587. /*ANSI_NULLS OFF  for after creation of sp_dboption*/
  1588.  
  1589. checkpoint
  1590. go
  1591.  
  1592. grant execute on sp_dboption to public
  1593. go
  1594. --------------------------------------------------------------------------
  1595. ------------------------------ sp_MS_marksystemobject -------------------------------
  1596. if object_id('sp_MS_marksystemobject', 'P') IS NOT NULL
  1597.     drop procedure sp_MS_marksystemobject
  1598. raiserror('create procedure sp_MS_marksystemobject ...',0,1)
  1599. go
  1600. -- FOR INTERNAL USE ONLY ... DO NOT DOCUMENT --
  1601. -- This procedure sets a bit in sysobjects.  This bit has no meaning, various
  1602. --    groups (starfigther, davinci, replication) use it for different things
  1603. -- MSQL makes no warranty, express or implied, on what objects will or will
  1604. --    not have this bit set.  Use at your own risk.
  1605. --   
  1606. create procedure sp_MS_marksystemobject
  1607.     @objname    nvarchar(517)  -- 517 is max for two part name
  1608. as
  1609.     -- pre-stuff --
  1610.     set nocount on
  1611.  
  1612.     -- CHECK THE OBJECT NAME --
  1613.     if object_id(@objname, 'local') is null
  1614.     begin
  1615.         raiserror('sp_MS_marksystemobject: Invalid object name ''%ls''',0,1,@objname)
  1616.         return 1
  1617.     end
  1618.  
  1619.     -- CHECK THE OBJECT OWNER (MUST BE A SYSTEM USER) --
  1620.     if user_name(ObjectProperty(object_id(@objname, 'local'), 'ownerid'))
  1621.                 not in ('dbo','INFORMATION_SCHEMA')
  1622.     begin
  1623.         raiserror('sp_MS_marksystemobject: Object must be owned by a system user.',0,1)
  1624.         return 1
  1625.     end
  1626.  
  1627.     -- DO THE UPDATE --
  1628.     begin tran
  1629.     dbcc LockObjectSchema(@objname)
  1630.     update sysobjects set status = status | 0xC0000000
  1631.                         where id = object_id(@objname, 'local')
  1632.     commit tran
  1633.     return @@error  -- sp_MS_marksystemobject
  1634. go
  1635. exec sp_MS_marksystemobject 'sp_MS_marksystemobject'
  1636. go
  1637. revoke execute on sp_MS_marksystemobject to public
  1638. go
  1639.  
  1640. --------------------------------------------------------------------------------
  1641. --    Turn CDOC for cross database ownership chaining on
  1642. --    Name has to match exactly
  1643. --------------------------------------------------------------------------------
  1644. go
  1645. exec sp_dboption 'msdb', 'DB Chaining', 'on'
  1646. go
  1647.  
  1648. if object_id('sp_linkedservers_rowset','P') IS NOT NULL
  1649.     drop procedure sp_linkedservers_rowset
  1650. go
  1651.  
  1652. create proc sp_linkedservers_rowset
  1653.     (
  1654.     @srvname     sysname
  1655.     )
  1656. as
  1657.     IF is_srvrolemember('sysadmin') = 1
  1658.     begin
  1659.         select
  1660.             SVR_NAME         = srvname,
  1661.             SVR_PRODUCT        = srvproduct,
  1662.             SVR_PROVIDERNAME    = providername,
  1663.             SVR_DATASOURCE        = datasource,
  1664.             SVR_PROVIDERSTRING = providerstring,
  1665.             SVR_LOCATION        = location,
  1666.             SVR_CATALOG        = catalog
  1667.         from master.dbo.sysservers
  1668.         where srvname = @srvname and (srvstatus & 128) = 128
  1669.         order by 1
  1670.     end
  1671.     ELSE
  1672.     begin
  1673.         select
  1674.             SVR_NAME         = srvname,
  1675.             SVR_PRODUCT        = srvproduct,
  1676.             SVR_PROVIDERNAME    = providername,
  1677.             SVR_DATASOURCE        = datasource,
  1678.             SVR_PROVIDERSTRING = NULL,
  1679.             SVR_LOCATION        = location,
  1680.             SVR_CATALOG        = catalog
  1681.         from master.dbo.sysservers
  1682.         where srvname = @srvname and (srvstatus & 128) = 128
  1683.         order by 1
  1684.     end
  1685. go
  1686.  
  1687.  
  1688. create proc sp_linkedservers_rowset;2
  1689. as
  1690.     IF is_srvrolemember('sysadmin') = 1
  1691.     begin
  1692.         select
  1693.             SVR_NAME         = srvname,
  1694.             SVR_PRODUCT        = srvproduct,
  1695.             SVR_PROVIDERNAME    = providername,
  1696.             SVR_DATASOURCE        = datasource,
  1697.             SVR_PROVIDERSTRING = providerstring,
  1698.             SVR_LOCATION        = location,
  1699.             SVR_CATALOG        = catalog
  1700.         from master.dbo.sysservers
  1701.         where (srvstatus & 128) = 128
  1702.         order by 1
  1703.     end
  1704.     ELSE
  1705.     begin
  1706.         select
  1707.             SVR_NAME         = srvname,
  1708.             SVR_PRODUCT        = srvproduct,
  1709.             SVR_PROVIDERNAME    = providername,
  1710.             SVR_DATASOURCE        = datasource,
  1711.             SVR_PROVIDERSTRING = NULL,
  1712.             SVR_LOCATION        = location,
  1713.             SVR_CATALOG        = catalog
  1714.         from master.dbo.sysservers
  1715.         where (srvstatus & 128) = 128
  1716.         order by 1
  1717.     end
  1718. go
  1719.  
  1720. grant execute on sp_linkedservers_rowset to public
  1721. go
  1722.  
  1723. if object_id('sp_linkedservers', 'P') is not null
  1724.     drop proc sp_linkedservers
  1725. go
  1726. raiserror(15339,-1,-1,'sp_linkedservers')
  1727. go
  1728. create proc sp_linkedservers as
  1729.     IF is_srvrolemember('sysadmin') = 1
  1730.     begin
  1731.         select
  1732.             SRV_NAME = srvname,
  1733.             SRV_PROVIDERNAME = providername,
  1734.             SRV_PRODUCT = srvproduct,
  1735.             SRV_DATASOURCE = datasource,
  1736.             SRV_PROVIDERSTRING = providerstring,
  1737.             SRV_LOCATION = location,
  1738.             SRV_CAT = catalog
  1739.         from master.dbo.sysservers
  1740.         order by 1
  1741.     end
  1742.     ELSE
  1743.     begin
  1744.         select
  1745.             SRV_NAME = srvname,
  1746.             SRV_PROVIDERNAME = providername,
  1747.             SRV_PRODUCT = srvproduct,
  1748.             SRV_DATASOURCE = datasource,
  1749.             SRV_PROVIDERSTRING = NULL,
  1750.             SRV_LOCATION = location,
  1751.             SRV_CAT = catalog
  1752.         from master.dbo.sysservers
  1753.         order by 1
  1754.     end
  1755. go
  1756.  
  1757. grant execute on sp_linkedservers to public
  1758. go
  1759.  
  1760. DENY SELECT (providerstring) ON sysservers TO PUBLIC
  1761. go
  1762.  
  1763.  
  1764. --------------------------------------------------------------------------------
  1765. --     SP_SETLOGIN USED BY AGENT
  1766. --------------------------------------------------------------------------------
  1767. if object_id('sp_setuserbylogin','X') IS NOT NULL
  1768.     exec sp_dropextendedproc 'sp_setuserbylogin'
  1769. execute sp_addextendedproc 'sp_setuserbylogin','(server internal)'
  1770. go
  1771. grant execute on sp_setuserbylogin to public
  1772. go
  1773. exec sp_MS_marksystemobject 'sp_setuserbylogin'
  1774. go
  1775. --------------------------------------------------------------------------------
  1776. --    ODBC/OLEDB Catalog Procs (INSTCAT.SQL)
  1777. --------------------------------------------------------------------------------
  1778.  
  1779. --------------------------------------------------------------------------------
  1780. --    End of SQLDMO System Procedures (SQLDMO.SQL)
  1781. --------------------------------------------------------------------------------
  1782.  
  1783.  
  1784. --------------------------------------------------------------------------------
  1785. -- END OF FILE: Turn off marking of system objects.
  1786. --    DO NOT ADD ANYTHING AFTER THIS POINT
  1787. --------------------------------------------------------------------------------
  1788. exec sp_MS_upd_sysobj_category 2
  1789. go
  1790.  
  1791. exec sp_configure 'allow updates',0
  1792. go
  1793.  
  1794. reconfigure with override
  1795. go
  1796.